
library(ComplexHeatmap)
library(circlize)  # for colorRamp2
library(grid)      # for unit(), gpar()

# 构造示例矩阵
set.seed(42)
mat <- matrix(rnorm(100), nrow = 10)
rownames(mat) <- paste0("G", 1:10)
colnames(mat) <- paste0("S", 1:10)

# 极简热图 + 暴露关键参数
Heatmap(
  mat,
  
  # — 图例与缺失值 —
  name    = "expr",   # 图例标题
  col     = colorRamp2( 
    c(min(mat), 0, max(mat)), 
    c("darkblue", "white", "darkred")
  ),
  na_col  = "#808080", # NA 值的颜色
  
  # — 聚类控制 —
  cluster_rows    = TRUE,          
  cluster_columns = TRUE,          
  clustering_distance_rows    = "pearson",  
  clustering_method_rows      = "ward.D2",   
  clustering_distance_columns = "pearson",    
  clustering_method_columns   = "ward.D2",    
  
  # — 行列名称显示 —
  show_row_names    = FALSE,    
  show_column_names = TRUE,    
  row_names_gp      = gpar(fontsize = 14),  
  column_names_gp   = gpar(fontsize = 14),  
  column_names_rot  = 45,                  
  
  # — 格子间距 & 栅格化 —
  column_gap    = unit(2, "mm"),  
  row_gap       = unit(2, "mm"),  
  use_raster    = TRUE,           
  raster_device = "png",          
  raster_quality= 2,              
  
  # — 图例参数 —
  heatmap_legend_param = list(
    title         = "Expression",
    at            = c(floor(min(mat)), 0, ceiling(max(mat))),
    labels        = c(floor(min(mat)), 0, ceiling(max(mat))),
    legend_height = unit(4, "cm"),
    title_gp      = gpar(fontsize = 14, fontface = "bold"),
    labels_gp     = gpar(fontsize = 12)
  )
)

